package com.thread;

/**
 * @author :admin
 * @description: TODO edit class Description
 * @create date: 2019/4/3 16:36
 */
public class DealthThread implements Runnable {
    public String username;
    public Object lockl = new Object();
    public Object lock2 = new Object();

    public void setFlag(String username) {
        this.username = username;
    }

    @Override
    public void run() {
        if (username.equals("a")) {
            synchronized (lockl) {
                try {
                    System.out.println("username=" + username);
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock2) {
                    System.out.println("按 lockl->lock2 代码顺序执行了");
                }
            }
        }
        if (username.equals("b")) {
            synchronized (lock2) {
                try {
                    System.out.println("username = " + username);
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lockl) {
                    System.out.println(" 按 lock2->lockl 代码顺序执行了  ");
                }
            }
        }
    }

    public static void main(String[] args) {
        try {
            DealthThread tl = new DealthThread();
            tl.setFlag("a");
            Thread threadl = new Thread(tl);
            threadl.start();
            Thread.sleep(100);
            tl.setFlag("b");
            Thread thread2 = new Thread(tl);
            thread2.start();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block e.printStackTrace();
        }
    }
}
